package cn.net.xyan.easy.spring.core.config;

import cn.net.xyan.easy.spring.core.config.value.ConfigKey;
import cn.net.xyan.easy.spring.core.config.value.IConfigValue;

/**
 * Created by zarra on 16/1/21.
 */
public abstract class AbstractAutoConfig implements IAutoConfig {

    IAutoConfigManager iAutoConfigManager;

    public enum ApplicationContextEnum{
        ROOT,MVC
    }

    protected String packageName(){
        return getClass().getPackage().getName();
    }

    protected String packageName(String prefix){
        return String.format("%s.%s",packageName(),prefix);
    }

    protected abstract void configManager();

    protected abstract void afterConfigManager();

    protected  void registerApplicationConfigClass(ApplicationContextEnum ace,Class<?> cls){
        IConfigValue configValue = null;
        switch (ace){
            case ROOT:
                configValue = setupKey(IAutoConfigManager.RootApplicationContextConfigClassKey);
                break;
            case MVC:
                configValue = setupKey(IAutoConfigManager.MVCApplicationContextConfigClassKey);
                break;
        }
       if (configValue!=null)
           configValue.enableValue(cls);
    }

    protected void registerRepositoryPackage(String packageName){
        IConfigValue repositoryScanPackages = setupKey(IAutoConfigManager.RepositoryScanPackagesKey);
        repositoryScanPackages.enableValue(packageName);
    }

    protected void registerEntityPackage(String packageName){
        IConfigValue modelScanPackagesModel = setupKey(IAutoConfigManager.EntityScanPackagesKey);
        modelScanPackagesModel.enableValue(packageName);
    }


    protected void putValue(ConfigKey key,Object value){
        IConfigValue maxRequestSize = setupKey(key);
        maxRequestSize.putValue(value);
    }

    protected IConfigValue setupKey(ConfigKey key){
        return  iAutoConfigManager.setupConfigValue(key);
    }

    protected IAutoConfigManager getiAutoConfigManager() {
        return iAutoConfigManager;
    }

    protected void setiAutoConfigManager(IAutoConfigManager iAutoConfigManager) {
        this.iAutoConfigManager = iAutoConfigManager;
    }

    @Override
    public void onStartupConfigManager(IAutoConfigManager manager) {
        this.iAutoConfigManager = manager;
        configManager();
    }

    @Override
    public void afterSetup(IAutoConfigManager manager) {
        this.iAutoConfigManager = manager;
        afterConfigManager();
    }
}
